# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=uv
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.7.8
pkgrel=1
pkgdesc='An extremely fast Python package installer and resolver, written in Rust (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/astral-sh/uv"
license=('spdx:Apache-2.0 OR MIT')
msys2_references=(
  'archlinux: uv'
  'purl: pkg:pypi/uv'
)
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-python-maturin"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-xz"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             'git')
options=('!strip')
source=("git+${url}.git#tag=${pkgver}"
        "zstd-sys.tar.gz::https://crates.io/api/v1/crates/zstd-sys/2.0.13+zstd.1.5.6/download"
        "bzip2-sys.tar.gz::https://crates.io/api/v1/crates/bzip2-sys/0.1.11+1.0.8/download"
        "bzip2-use-pkgconfig.patch"
        "link-zstd-dynamically.patch")
sha256sums=('eceadf1bea8fbdbed4c69f460365b86398f305330105ca285c19593a963243ac'
            '38ff0f21cfee8f97d94cef41359e0c89aa6113028ab0291aa8ca0038995a95aa'
            '736a955f3fa7875102d57c82b8cac37ec45224a07fd32d58f9f7a186b6cd4cdc'
            'f73309fb98b89b2085bccee6ffb66820082628d096bc989922b25d33a1f5f378'
            '48f4900ceb02d3aaf9a1020f33d56629156e96759f456c0e7ca18bfcf910767b')

prepare() {
  cd "${_realname}"

  patch -d "../zstd-sys-2.0.13+zstd.1.5.6" -i "../link-zstd-dynamically.patch"
  patch -d "../bzip2-sys-0.1.11+1.0.8" -i "../bzip2-use-pkgconfig.patch"
  cat >> Cargo.toml <<END

[patch.crates-io]
bzip2-sys = { path = "../bzip2-sys-0.1.11+1.0.8" }
zstd-sys = { path = "../zstd-sys-2.0.13+zstd.1.5.6" }
END

  rm rust-toolchain.toml
  cargo update -p zstd-sys -p bzip2-sys
  cargo fetch --locked
}

build() {
  cd "${_realname}"

  export PKG_CONFIG_ALL_DYNAMIC=1
  export ZSTD_SYS_USE_PKG_CONFIG=1
  export WINAPI_NO_BUNDLED_LIBRARIES=1
  export RUSTFLAGS="${RUSTFLAGS/+crt-static/-crt-static}"
  export _PYTHON_HOST_PLATFORM=$(python -c "import sysconfig, sys; sys.stdout.write(sysconfig.get_platform())")
  maturin build --frozen --strip --release --all-features
}

package_uv() {
  install -Dm755 "${_realname}/target/release/uv"{,x} -t "${pkgdir}${MINGW_PREFIX}/bin/"

  local _complete="${_realname}/target/release/uv generate-shell-completion"
  $_complete bash | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/uv"
  $_complete fish | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/uv.fish"
  $_complete zsh | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_uv"

  install -Dm644 "${_realname}"/LICENSE-{MIT,APACHE} -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
}

package_python-uv() {
  depends=("${MINGW_PACKAGE_PREFIX}-python" "${MINGW_PACKAGE_PREFIX}-uv")

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" "${_realname}"/target/wheels/*.whl

  rm -rf "${pkgdir}${MINGW_PREFIX}/bin"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
